package tv.youxing.android.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import tv.youxing.android.Interface.CustomToobarImp;
import tv.youxing.android.R;
import tv.youxing.android.custom.CustomToolbar;
import tv.youxing.android.entity.AdvertisementDetailsModel;
import tv.youxing.android.entity.VideoPlayerPostModel;
import tv.youxing.android.utils.GsonUtils;
import tv.youxing.android.utils.PicassoUtils;
import tv.youxing.android.utils.ShareUtils;
import tv.youxing.android.utils.StaticClass;

/**
 * Created by Administrator on 2017/7/3.
 * 广告详情
 */

public class AdvertisementDetailsActivity extends BaseActivity {


    @BindView(R.id.rv)
    GridView mGridView;
    @BindView(R.id.tb)
    CustomToolbar mCustomToolbar;

    int ad_id = -1;

    AdvertisementDetailsModel alldata;
    List<AdvertisementDetailsModel.ResultBean.DataBean> listDatas = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advertisementdetails);
        ButterKnife.bind(this);

        this.ad_id = Integer.parseInt(getIntent().getStringExtra("ad_id"));

        initView();
        loadDatas();


    }

    private void initView() {

        mCustomToolbar.setBackClickLister(new CustomToobarImp.back() {
            @Override
            public void onclick() {
                finish();
            }
        });
    }

    private void loadDatas() {
        //post请求简洁版实现
        HttpParams params = new HttpParams();
        params.put("ad_id", this.ad_id);

        RxVolley.post(StaticClass.AdvertisementDetailsURL, params, new HttpCallback() {
            @Override
            public void onSuccess(String t) {
                alldata = GsonUtils.GsonToBean(t, AdvertisementDetailsModel.class);
                listDatas = alldata.getResult().getData();

                initData();
            }
        });
    }

    private void initData() {
        mGridView.setAdapter(new Adapter(this, listDatas));
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                AdvertisementDetailsModel.ResultBean.DataBean m = listDatas.get(i);
                VideoPlayerPostModel model = new VideoPlayerPostModel();
                model.setMultimedia_id(m.getMultimedia_id());
                model.setTitle(m.getTitle());
                model.setIntro(m.getIntro());
                model.setCover(m.getCover());
                model.setDuration(m.getDuration());
                model.setPublish_at(m.getPublish_at());
                model.setPlay_url(m.getPlay_url());
                model.setType(m.getType());
                model.setPlay_count(m.getPlay_count());
                model.setComment_count(m.getComment_count());
                model.setLike_count(m.getLike_count());
                model.setCollection_count(m.getCollection_count());
                model.setStatus(m.getStatus());
                model.setCreated_at(m.getCreated_at());
                model.setUpdated_at(m.getUpdated_at());
                model.setAlbum_name(m.getAlbum_name());
                model.setIs_collectied(m.getIs_collectied());
                model.setIs_like(m.getIs_like());
                VideoPlayerPostModel.AlbumBean albumBean = new VideoPlayerPostModel.AlbumBean();
                albumBean.setAlbum_id(m.getAlbum().getAlbum_id());
                albumBean.setName(m.getAlbum().getName());
                albumBean.setCover(m.getAlbum().getCover());
                albumBean.setIs_order(m.getAlbum().getIs_order());
                model.setAlbum(albumBean);

                Intent intent = new Intent(AdvertisementDetailsActivity.this, SubscribeDeteilsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("data",model);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }


    class Adapter extends BaseAdapter {

        private Context context;
        private List<AdvertisementDetailsModel.ResultBean.DataBean> listData = new ArrayList<>();
        private LayoutInflater layoutInflater;


        public Adapter(Context context, List<AdvertisementDetailsModel.ResultBean.DataBean> listData) {
            this.context = context;
            this.listData = listData;
            layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return listData == null || listData.size() == 0 ? 0 : listData.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            ViewHolder viewHolder = null;
            if (viewHolder == null) {
                viewHolder = new ViewHolder();
                convertView = layoutInflater.inflate(R.layout.layout_item_video, null);
                viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
                viewHolder.tvName = (TextView) convertView.findViewById(R.id.tvName);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            int width = ShareUtils.getInt(context, "width", 100);
            PicassoUtils.loadImageViewSize(context, listData.get(position).getCover(), (width - 6) / 2, (width - 6) / 2, R.drawable.logo, viewHolder.ivIcon);
            viewHolder.tvName.setText(listData.get(position).getTitle() + listData.get(position).getMultimedia_id());
            viewHolder.tvName.setLines(1);

            return convertView;
        }


        class ViewHolder {
            private ImageView ivIcon;
            private TextView tvName;
        }
    }
}
